package com.yadang.choujiang;

/**
 * @author: 亚当
 * @date: 2023年05月14日 16:48
 * @package: com.yadang.choujiang
 * @Description: TODO
 */
import java.util.*;

public class Lottery1 {
    static class Prize {
        String name;
        int weight;

        public Prize(String name, int weight) {
            this.name = name;
            this.weight = weight;
        }
    }

    public static void main(String[] args) {
        List<Prize> prizes = new ArrayList<>();
        prizes.add(new Prize("三等奖", 85)); // 三等奖的概率为85/100
        prizes.add(new Prize("二等奖", 10)); // 二等奖的概率为10/100
        prizes.add(new Prize("一等奖", 5)); // 一等奖的概率为5/100
        for (int i=0;i<100;i++){
            Prize winningPrize = drawPrize(prizes);
            System.out.println("第"+(i+1)+"次抽奖。恭喜你，你获得了" + winningPrize.name);
        }

    }

    public static Prize drawPrize(List<Prize> prizes) {
        int totalWeight = 0;
        for (Prize prize : prizes) {
            totalWeight += prize.weight;
        }

        int randomNum = new Random().nextInt(totalWeight) + 1;
        int cumWeight = 0;
        for (Prize prize : prizes) {
            cumWeight += prize.weight;
            if (randomNum <= cumWeight) {
                return prize;
            }
        }

        // should never reach this point if weights are correct
        return null;
    }
}
